package training.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.slim3.datastore.Datastore;
import org.slim3.util.BeanUtil;
import com.google.appengine.api.datastore.Transaction;

import training.meta.VoteResultMeta;
import training.model.VoteResult;

public class RegistService {

    // tweet参考登録用
    public VoteResult vote(Map<String, Object> input) {
        VoteResult vote = new VoteResult();
        BeanUtil.copy(input, vote);
        Transaction tx = Datastore.beginTransaction();
        Datastore.put(vote);
        tx.commit();
        return vote;
    }

    private VoteResultMeta t = new VoteResultMeta();

    // public void getResultList(int resultCount1[]) {
    //
    // List<VoteResult>
    // resultList=Datastore.query(t).sort(t.selection.desc).asList();
    // for(VoteResult VoteResult :resultList){
    //
    // switch(Integer.parseInt(VoteResult.getSelection())){
    // case 1:
    // resultCount1[0]++;
    // break;
    // case 2:
    // resultCount1[1]++;
    // break;
    // case 3:
    // resultCount1[2]++;
    // break;
    // }
    //
    // }
    // }
    public List<VoteResult> getVoteResultList() {
        return Datastore.query(t).sort(t.selection.desc).asList();
    }
    /*//投票回数カウント
    public int getResult(List<VoteResult> resultList, String select) {

        int cnt = 0;
        for (VoteResult result : resultList) {
            if (result.getSelection().equals(select)) {
                cnt++;
            }
        }
        return cnt;
    }*/
    //投票回数カウント
    public int getResult2(String select) {
        try{
        List<VoteResult>list = Datastore.query(t).filter(t.selection.equal(select)).sort(t.selection.asc).asList();
        int cnt =list.size();
        return cnt;
        }catch(Exception e){
            System.out.println("エラーが発生しました");
            return 0;
        }
    }
    /*//コメント取得
    public List<String> getComment(List<VoteResult> resultList, String select) {
         List<String> commentList = new ArrayList<String>();
        for (VoteResult result : resultList) {
            if (result.getSelection().equals(select)) {
                commentList.add(result.getComment());
            }
        }
        return commentList;
    }*/
    //コメント取得
    public List<String> getComment2(String select) {
        try{
        List<String> commentList = new ArrayList<String>();
        List<VoteResult>list = Datastore.query(t).filter(t.selection.equal(select)).sort(t.selection.asc).asList();
        for (VoteResult result : list) {
            commentList.add(result.getComment());
        }
        return commentList;
        }catch(Exception e){
            System.out.println("エラーが発生しました");
            return null;
        }
    }
}
